<?php
//用户管理

namespace App\Services;

use App\Common\CommonConst;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;

class UploadService
{


    private static function _checkFile ($file)
    {
        /*$width = 1500;
        $height = 1000;
        $sizeMax = 2097152; //2m
        $sizeMaxText = '2m';
        $fileSizeRes = getimagesize($file);
        $fileWidth = $fileSizeRes[0];
        $fileHeight = $fileSizeRes[1];
        if(($width && $fileWidth != $width) || ($height && $fileHeight != $height)) {
            return resultError('图片尺寸不符合要求');
        } else if($file->getSize() > $sizeMax) {
            return resultError('图片过大，不可超过' . $sizeMaxText);
        }*/
        return true;
    }

    /**
     * @param $type
     * @param $file
     * @return array
     */
    public static function uploadPic($file)
    {
        //验证图片宽高
        $checkResult = self::_checkFile($file);
        if ($checkResult) {
            $now = Carbon::now();
            $fileName = $now->getTimeStamp() . $now->micro . $file->hashName();
            $path = Arr::join([
                'stockimg',
                'uploads',
                'pic',
                $now->year,
                $now->month,
                $now->day,
            ], '/');
            $qiniuDomain = config('filesystems.disks.qiniu.url');
            $resFileName = Storage::disk('qiniu')->putFileAs($path, $file, $fileName);
            if (!$resFileName) {
                return resultError('上传失败，请稍后再试');
            }
            return resultSuccess([
                'fileUrl' => $qiniuDomain . $resFileName,
            ]);
        }
        return $checkResult;
    }

}
